<?php
include('include/config.inc.php');
$toDate   = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'];
$fromDate = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];
$selectSMS = "SELECT smsfacility.mobileNum, user.userName, smsfacility.message, technician.firstName AS companyName, technician.lastName, technician.userType
                FROM smsfacility
                LEFT JOIN technician ON technician.technicianId = smsfacility.technicianId 
                LEFT JOIN user ON user.userId = smsfacility.uId 
               WHERE smsfacility.date >='".$fromDate."' AND smsfacility.date <='".$toDate."'
               UNION
              SELECT smsfacility.mobileNum,user.userName,smsfacility.message, party.companyName, NULL AS lastName, NULL AS userType
                FROM smsfacility
                LEFT JOIN party ON smsfacility.partyId = party.partyId
                LEFT JOIN user ON user.userId = smsfacility.uId 
               WHERE smsfacility.date >='".$fromDate."' AND smsfacility.date <='".$toDate."'
               UNION
              SELECT smsfacility.mobileNum,user.userName,smsfacility.message, supplier.companyName, NULL AS lastName, NULL AS userType
                FROM smsfacility
                LEFT JOIN supplier ON smsfacility.supplierId = supplier.supplierId
                LEFT JOIN user ON user.userId = smsfacility.uId 
               WHERE smsfacility.date >='".$fromDate."' AND smsfacility.date <='".$toDate."'
               UNION
              SELECT smsfacility.mobileNum,user.userName,smsfacility.message, NULL AS companyName, NULL AS lastName, NULL AS userType
                FROM smsfacility
                LEFT JOIN user ON user.userId = smsfacility.uId 
               WHERE smsfacility.date >='".$fromDate."' AND smsfacility.date <='".$toDate."'
               ";
$smsRes = mysql_query($selectSMS);
$smsCounter = 0;
$smsAarray  = array();
while($smsListRes = mysql_fetch_array($smsRes))
{
  if($smsListRes['userType'] == "None")
    $smsAarray[$smsCounter]['techName']  = $smsListRes['companyName']."".$smsListRes['lastName'];
  else
    $smsAarray[$smsCounter]['techName']  = $smsListRes['companyName']." ".$smsListRes['lastName']."<b>-".$smsListRes['userType'];
  $smsAarray[$smsCounter]['mobileNum'] = $smsListRes['mobileNum'];
  $smsAarray[$smsCounter]['userName'] = $smsListRes['userName'];
  $smsAarray[$smsCounter]['message'] = $smsListRes['message'];
  $smsCounter++;
}
$smarty->assign("smsAarray",$smsAarray);
$smarty->display("smsListAj.tpl");
?>